﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Win.CSkin.Test
{
    /// <summary>
    /// 枚举帮助类
    /// </summary>
    public class EnumHelper
    {
        /// <summary>
        /// 根据枚举值获取对应枚举值名称
        /// </summary>
        /// <returns></returns>
        public static object GetNameByValue<T>(int value)
        {
            return Enum.ToObject(typeof(T), value);
        }

        /// <summary>
        /// 根据枚举值获取描述
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDesByValue<T>(int value)
        {
            try
            {
                object obj = GetNameByValue<T>(value);
                T temp = (T)Enum.Parse(typeof(T), obj.ToString());
                return GetDescription(temp);
            }
            catch
            {
                return "未知";
            }
        }

        /// <summary>  
        /// 获取枚举的描述  
        /// </summary>  
        /// <param name="en">枚举</param>  
        /// <returns>返回枚举的描述</returns>  
        public static string GetDescription<T>(T en)
        {
            Type type = en.GetType();   //获取类型  
            MemberInfo[] memberInfos = type.GetMember(en.ToString());   //获取成员  
            if (memberInfos != null && memberInfos.Length > 0)
            {
                DescriptionAttribute[] attrs = memberInfos[0].GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];   //获取描述特性  

                if (attrs != null && attrs.Length > 0)
                {
                    return attrs[0].Description;    //返回当前描述  
                }
            }
            return en.ToString();
        }
        /// <summary>
        /// 根据枚举、项的value值返回枚举的Description
        /// </summary>
        /// <param name="enumType">枚举</param>
        /// <param name="enumIntValue">项的value值</param>
        /// <returns></returns>
        public static string GetDescription(Type enumType, int enumIntValue)
        {
            //string nameDesc = System.Enum.GetName(enumType, enumIntValue);
            return GetDescription((System.Enum)System.Enum.ToObject(enumType, enumIntValue));
        }
        /// <summary>
        /// enum TO List
        /// </summary>
        /// <param name="enumType">要操作的枚举对象</param>
        /// <returns>List EnumInfo </returns>
        public static List<EnumInfo> EnumToEnumInfoList(Type enumType)
        {
            List<EnumInfo> enumInfoList = new List<EnumInfo>();
            EnumInfo enumInfo;
            foreach (int code in System.Enum.GetValues(enumType))
            {
                try
                {
                    string itemName = System.Enum.GetName(enumType, code);
                    string itemValue = code.ToString();
                    string itemDescription = GetDescription(enumType, code);

                    enumInfo = new EnumInfo();
                    enumInfo.itemName = itemName;
                    enumInfo.itemValue = itemValue;
                    enumInfo.itemDescription = itemDescription;
                    enumInfoList.Add(enumInfo);
                }
                catch (Exception ex) { }
            }
            return enumInfoList;
        }
    }

    public class EnumInfo
    {
        private string _itemName;
        private string _itemValue;
        private string _itemDescription;

        /// <summary>
        /// item项
        /// </summary>
        public string itemName
        {
            get { return _itemName; }
            set { _itemName = value; }
        }

        /// <summary>
        /// item值
        /// </summary>
        public string itemValue
        {
            get { return _itemValue; }
            set { _itemValue = value; }
        }

        /// <summary>
        ///  item项描述
        /// </summary>
        public string itemDescription
        {
            get { return _itemDescription; }
            set { _itemDescription = value; }
        }
    }
}
